/**
 *
 */
package com.cmcc.pcap.packet.tcp.https;

import java.util.Date;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.cmcc.packet.entity.XdrEntity;
import com.cmcc.pcap.packet.tcp.TcpConnection;
import com.cmcc.pcap.packet.tcp.TcpProtocolHandler;
import com.cmcc.pcap.packet.tcp.http.HttpTransaction;
import com.cmcc.pcap.packet.tcp.https.xdr.HttpsXdrGenerate;

/**
 * @Date 2018年3月21日 上午10:06:33
 * @author Amish
 *
 */
public class HttpsHandler extends TcpProtocolHandler<XdrEntity> {
//	@Autowired
//	private  HttpTransactionDao httpTransactionDao;
	private static Logger logger = LogManager.getLogger(HttpsHandler.class);
	private HttpsXdrGenerate hsxg;

	public HttpsHandler() {
		hsxg = new HttpsXdrGenerate();
	}
	
	@Override
	public List<XdrEntity> handle(TcpConnection tcpConnection,String taskId) {
		
		logger.debug("start execute https handler");
		
		logger.debug(new Date(tcpConnection.lastPacketTime));//最后一个packet的处理时间(注:非抓包时间)

		
		/**
		 * Connection 内TCP 分段(segment)处理：乱序重组、重传清洗、TODO 默认保留前2个分段且可配？
		 */
		logger.debug("start https connection deal");
		tcpConnection.tcpConnectionPacketsDeal();//duplicate ack merge、retransmission and spurious retransmission merge、segments reassemble
		logger.debug("stop https connection deal");
		
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (initial)  : "+tcpConnection.packetsNumInitial);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after duplicate ack merge)  : "+tcpConnection.packetsNumAfterDuplicateAckMerge);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after retransmission merge)  : "+tcpConnection.packetsNumAfterRetransmissionMerge);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after reorder packets)  : "+tcpConnection.packetsNumAfterReorderTcpPacket);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> hava packets (after reassemble) : "+tcpConnection.packetsNumAfterReassemble);
		
//		for(TcpPacketDecorator tpd : tcpConnection.al){
//			logger.error("-----------------------------------");
//			logger.error(new String(tpd.getData()));
//			logger.error("-----------------------------------");
//		}
//		for(TcpPacketDecorator tpd : tcpConnection.retransmissionMergeAl){
//			logger.error("++++++++++++++++++++++++++++++++++++");
//			logger.error(new String(tpd.getData()));
//			logger.error("++++++++++++++++++++++++++++++++++++");
//		}
//		for(TcpPacketDecorator tpd : tcpConnection.reassembleAl){
//			logger.error("====================================");
//			logger.error(new String(tpd.getData()));
//			logger.error("====================================");
//		}
		
		
		/**
		 * TODO HTTP事务识别：按HTTP事务重新对TcpConnection进行划分成多个TcpConnection
		 * 握手信息放在第一个HTTP事务中
		 * 拆链信息放在最后一个HTTP事务中
		 */

		HttpTransaction httpsTransaction = HttpsStatistics.httpsstatistics(tcpConnection);

		List<XdrEntity> xdrhttpsEntityList = hsxg.generate(httpsTransaction,taskId);
		logger.debug("this connection <"+tcpConnection.connectionKey+"> save into httpsentity !");
		
//		XdrGenerateService xdrGenerateService = ProtocolSpringContextUtils.getBean("xdrGenerateServiceImpl");
//		xdrGenerateService.insert(xdrhttpsEntityList);

		//TODO 处理https的信息
		logger.debug("this connection <"+tcpConnection.connectionKey+"> saved into database !");
		
		return xdrhttpsEntityList;
		
	}

}

